<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Estimating Sample Sizes for the Negative Binomial.</title>
<link rel="stylesheet" href="../../../../math.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../../../../index.html" title="Math Toolkit 4.2.1">
<link rel="up" href="../neg_binom_eg.html" title="Negative Binomial Distribution Examples">
<link rel="prev" href="neg_binom_conf.html" title="Calculating Confidence Limits on the Frequency of Occurrence for the Negative Binomial Distribution">
<link rel="next" href="negative_binomial_example1.html" title="Negative Binomial Sales Quota Example.">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../../../boost.png"></td>
<td align="center"><a href="../../../../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="neg_binom_conf.html"><img src="../../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../neg_binom_eg.html"><img src="../../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../../index.html"><img src="../../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="negative_binomial_example1.html"><img src="../../../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h5 class="title">
<a name="math_toolkit.stat_tut.weg.neg_binom_eg.neg_binom_size_eg"></a><a class="link" href="neg_binom_size_eg.html" title="Estimating Sample Sizes for the Negative Binomial.">Estimating
          Sample Sizes for the Negative Binomial.</a>
</h5></div></div></div>
<p>
            Imagine you have an event (let's call it a "failure" - though
            we could equally well call it a success if we felt it was a 'good' event)
            that you know will occur in 1 in N trials. You may want to know how many
            trials you need to conduct to be P% sure of observing at least k such
            failures. If the failure events follow a negative binomial distribution
            (each trial either succeeds or fails) then the static member function
            <code class="computeroutput"><span class="identifier">negative_binomial_distibution</span><span class="special">&lt;&gt;::</span><span class="identifier">find_minimum_number_of_trials</span></code>
            can be used to estimate the minimum number of trials required to be P%
            sure of observing the desired number of failures.
          </p>
<p>
            The example program <a href="../../../../../../example/neg_binomial_sample_sizes.cpp" target="_top">neg_binomial_sample_sizes.cpp</a>
            demonstrates its usage.
          </p>
<p>
            It centres around a routine that prints out a table of minimum sample
            sizes (number of trials) for various probability thresholds:
          </p>
<pre class="programlisting"><span class="keyword">void</span> <span class="identifier">find_number_of_trials</span><span class="special">(</span><span class="keyword">double</span> <span class="identifier">failures</span><span class="special">,</span> <span class="keyword">double</span> <span class="identifier">p</span><span class="special">);</span>
</pre>
<p>
            First define a table of significance levels: these are the maximum acceptable
            probability that <span class="emphasis"><em>failure</em></span> or fewer events will be
            observed.
          </p>
<pre class="programlisting"><span class="keyword">double</span> <span class="identifier">alpha</span><span class="special">[]</span> <span class="special">=</span> <span class="special">{</span> <span class="number">0.5</span><span class="special">,</span> <span class="number">0.25</span><span class="special">,</span> <span class="number">0.1</span><span class="special">,</span> <span class="number">0.05</span><span class="special">,</span> <span class="number">0.01</span><span class="special">,</span> <span class="number">0.001</span><span class="special">,</span> <span class="number">0.0001</span><span class="special">,</span> <span class="number">0.00001</span> <span class="special">};</span>
</pre>
<p>
            Confidence value as % is (1 - alpha) * 100, so alpha 0.05 == 95% confidence
            that the desired number of failures will be observed. The values range
            from a very low 0.5 or 50% confidence up to an extremely high confidence
            of 99.999.
          </p>
<p>
            Much of the rest of the program is pretty-printing, the important part
            is in the calculation of minimum number of trials required for each value
            of alpha using:
          </p>
<pre class="programlisting"><span class="special">(</span><span class="keyword">int</span><span class="special">)</span><span class="identifier">ceil</span><span class="special">(</span><span class="identifier">negative_binomial</span><span class="special">::</span><span class="identifier">find_minimum_number_of_trials</span><span class="special">(</span><span class="identifier">failures</span><span class="special">,</span> <span class="identifier">p</span><span class="special">,</span> <span class="identifier">alpha</span><span class="special">[</span><span class="identifier">i</span><span class="special">]);</span>
</pre>
<p>
            find_minimum_number_of_trials returns a double, so <code class="computeroutput"><span class="identifier">ceil</span></code>
            rounds this up to ensure we have an integral minimum number of trials.
          </p>
<pre class="programlisting"><span class="keyword">void</span> <span class="identifier">find_number_of_trials</span><span class="special">(</span><span class="keyword">double</span> <span class="identifier">failures</span><span class="special">,</span> <span class="keyword">double</span> <span class="identifier">p</span><span class="special">)</span>
<span class="special">{</span>
   <span class="comment">// trials = number of trials</span>
   <span class="comment">// failures = number of failures before achieving required success(es).</span>
   <span class="comment">// p        = success fraction (0 &lt;= p &lt;= 1.).</span>
   <span class="comment">//</span>
   <span class="comment">// Calculate how many trials we need to ensure the</span>
   <span class="comment">// required number of failures DOES exceed "failures".</span>

  <span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"\n"</span><span class="string">"Target number of failures = "</span> <span class="special">&lt;&lt;</span> <span class="special">(</span><span class="keyword">int</span><span class="special">)</span><span class="identifier">failures</span><span class="special">;</span>
  <span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">",   Success fraction = "</span> <span class="special">&lt;&lt;</span> <span class="identifier">fixed</span> <span class="special">&lt;&lt;</span> <span class="identifier">setprecision</span><span class="special">(</span><span class="number">1</span><span class="special">)</span> <span class="special">&lt;&lt;</span> <span class="number">100</span> <span class="special">*</span> <span class="identifier">p</span> <span class="special">&lt;&lt;</span> <span class="string">"%"</span> <span class="special">&lt;&lt;</span> <span class="identifier">endl</span><span class="special">;</span>
   <span class="comment">// Print table header:</span>
   <span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"____________________________\n"</span>
           <span class="string">"Confidence        Min Number\n"</span>
           <span class="string">" Value (%)        Of Trials \n"</span>
           <span class="string">"____________________________\n"</span><span class="special">;</span>
   <span class="comment">// Now print out the data for the alpha table values.</span>
  <span class="keyword">for</span><span class="special">(</span><span class="keyword">unsigned</span> <span class="identifier">i</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="identifier">i</span> <span class="special">&lt;</span> <span class="keyword">sizeof</span><span class="special">(</span><span class="identifier">alpha</span><span class="special">)/</span><span class="keyword">sizeof</span><span class="special">(</span><span class="identifier">alpha</span><span class="special">[</span><span class="number">0</span><span class="special">]);</span> <span class="special">++</span><span class="identifier">i</span><span class="special">)</span>
   <span class="special">{</span> <span class="comment">// Confidence values %:</span>
      <span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">fixed</span> <span class="special">&lt;&lt;</span> <span class="identifier">setprecision</span><span class="special">(</span><span class="number">3</span><span class="special">)</span> <span class="special">&lt;&lt;</span> <span class="identifier">setw</span><span class="special">(</span><span class="number">10</span><span class="special">)</span> <span class="special">&lt;&lt;</span> <span class="identifier">right</span> <span class="special">&lt;&lt;</span> <span class="number">100</span> <span class="special">*</span> <span class="special">(</span><span class="number">1</span><span class="special">-</span><span class="identifier">alpha</span><span class="special">[</span><span class="identifier">i</span><span class="special">])</span> <span class="special">&lt;&lt;</span> <span class="string">"      "</span>
      <span class="comment">// find_minimum_number_of_trials</span>
      <span class="special">&lt;&lt;</span> <span class="identifier">setw</span><span class="special">(</span><span class="number">6</span><span class="special">)</span> <span class="special">&lt;&lt;</span> <span class="identifier">right</span>
      <span class="special">&lt;&lt;</span> <span class="special">(</span><span class="keyword">int</span><span class="special">)</span><span class="identifier">ceil</span><span class="special">(</span><span class="identifier">negative_binomial</span><span class="special">::</span><span class="identifier">find_minimum_number_of_trials</span><span class="special">(</span><span class="identifier">failures</span><span class="special">,</span> <span class="identifier">p</span><span class="special">,</span> <span class="identifier">alpha</span><span class="special">[</span><span class="identifier">i</span><span class="special">]))</span>
      <span class="special">&lt;&lt;</span> <span class="identifier">endl</span><span class="special">;</span>
   <span class="special">}</span>
   <span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">endl</span><span class="special">;</span>
<span class="special">}</span> <span class="comment">// void find_number_of_trials(double failures, double p)</span>
</pre>
<p>
            finally we can produce some tables of minimum trials for the chosen confidence
            levels:
          </p>
<pre class="programlisting"><span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span>
<span class="special">{</span>
    <span class="identifier">find_number_of_trials</span><span class="special">(</span><span class="number">5</span><span class="special">,</span> <span class="number">0.5</span><span class="special">);</span>
    <span class="identifier">find_number_of_trials</span><span class="special">(</span><span class="number">50</span><span class="special">,</span> <span class="number">0.5</span><span class="special">);</span>
    <span class="identifier">find_number_of_trials</span><span class="special">(</span><span class="number">500</span><span class="special">,</span> <span class="number">0.5</span><span class="special">);</span>
    <span class="identifier">find_number_of_trials</span><span class="special">(</span><span class="number">50</span><span class="special">,</span> <span class="number">0.1</span><span class="special">);</span>
    <span class="identifier">find_number_of_trials</span><span class="special">(</span><span class="number">500</span><span class="special">,</span> <span class="number">0.1</span><span class="special">);</span>
    <span class="identifier">find_number_of_trials</span><span class="special">(</span><span class="number">5</span><span class="special">,</span> <span class="number">0.9</span><span class="special">);</span>

    <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
<span class="special">}</span> <span class="comment">// int main()</span>
</pre>
<div class="note"><table border="0" summary="Note">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../../../../doc/src/images/note.png"></td>
<th align="left">Note</th>
</tr>
<tr><td align="left" valign="top">
<p>
              Since we're calculating the <span class="emphasis"><em>minimum</em></span> number of
              trials required, we'll err on the safe side and take the ceiling of
              the result. Had we been calculating the <span class="emphasis"><em>maximum</em></span>
              number of trials permitted to observe less than a certain number of
              <span class="emphasis"><em>failures</em></span> then we would have taken the floor instead.
              We would also have called <code class="computeroutput"><span class="identifier">find_minimum_number_of_trials</span></code>
              like this:
            </p>
<pre class="programlisting"><span class="identifier">floor</span><span class="special">(</span><span class="identifier">negative_binomial</span><span class="special">::</span><span class="identifier">find_minimum_number_of_trials</span><span class="special">(</span><span class="identifier">failures</span><span class="special">,</span> <span class="identifier">p</span><span class="special">,</span> <span class="identifier">alpha</span><span class="special">[</span><span class="identifier">i</span><span class="special">]))</span>
</pre>
<p>
              which would give us the largest number of trials we could conduct and
              still be P% sure of observing <span class="emphasis"><em>failures or less</em></span>
              failure events, when the probability of success is <span class="emphasis"><em>p</em></span>.
            </p>
</td></tr>
</table></div>
<p>
            We'll finish off by looking at some sample output, firstly suppose we
            wish to observe at least 5 "failures" with a 50/50 (0.5) chance
            of success or failure:
          </p>
<pre class="programlisting">Target number of failures = 5,   Success fraction = 50%

____________________________
Confidence        Min Number
 Value (%)        Of Trials
____________________________
    50.000          11
    75.000          14
    90.000          17
    95.000          18
    99.000          22
    99.900          27
    99.990          31
    99.999          36

</pre>
<p>
            So 18 trials or more would yield a 95% chance that at least our 5 required
            failures would be observed.
          </p>
<p>
            Compare that to what happens if the success ratio is 90%:
          </p>
<pre class="programlisting">Target number of failures = 5.000,   Success fraction = 90.000%

____________________________
Confidence        Min Number
 Value (%)        Of Trials
____________________________
    50.000          57
    75.000          73
    90.000          91
    95.000         103
    99.000         127
    99.900         159
    99.990         189
    99.999         217
</pre>
<p>
            So now 103 trials are required to observe at least 5 failures with 95%
            certainty.
          </p>
</div>
<div class="copyright-footer">Copyright © 2006-2021 Nikhar Agrawal, Anton Bikineev, Matthew Borland,
      Paul A. Bristow, Marco Guazzone, Christopher Kormanyos, Hubert Holin, Bruno
      Lalande, John Maddock, Evan Miller, Jeremy Murphy, Matthew Pulver, Johan Råde,
      Gautam Sewani, Benjamin Sobotta, Nicholas Thompson, Thijs van den Berg, Daryle
      Walker and Xiaogang Zhang<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="neg_binom_conf.html"><img src="../../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../neg_binom_eg.html"><img src="../../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../../index.html"><img src="../../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="negative_binomial_example1.html"><img src="../../../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
